package com.glodon.paas.account.dao.mysql;

import java.util.ArrayList;
import java.util.List;

import org.jasig.cas.services.RegisteredService;
import org.jasig.cas.services.ServiceRegistryDao;
import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.account.dao.domain.RegisteredServiceEntity;
import com.glodon.paas.account.dao.mapper.RegisteredServiceEntityMapper;
import com.glodon.paas.account.service.util.PaasRegisteredServiceImpl;

public class JdbcServiceRegistryDaoImpl implements ServiceRegistryDao{
 
	@Autowired
	private RegisteredServiceEntityMapper registeredServiceEntityMapper;
	
	
	@Override
	public boolean delete(RegisteredService registeredService) {		
		return registeredServiceEntityMapper.deleteByPrimaryKey(registeredService.getId()) > 0;
         
    }
	@Override
    public RegisteredService findServiceById(final long id) {	
		return convert(registeredServiceEntityMapper.selectByPrimaryKey(id));
		
    }
	@Override
    public List<RegisteredService> load() {
       List<RegisteredService> serviceList = new ArrayList<RegisteredService>();
       List<RegisteredServiceEntity> entityList = registeredServiceEntityMapper.selectAll();
       for(RegisteredServiceEntity e : entityList){
    	   serviceList.add(convert(e));
       }
       return serviceList;
    }
	@Override
    public RegisteredService save(final RegisteredService registeredService) {
/*		if(registeredService.getId() == -1){
			((AbstractRegisteredService) registeredService).setId(findHighestId()+1);
		}*/
		RegisteredServiceEntity entity = convert(registeredService);
		registeredServiceEntityMapper.insertSelective(entity);
        return convert(entity);
    }
    
    private RegisteredServiceEntity convert(RegisteredService svr){  
    	if(svr != null){
        	PaasRegisteredServiceImpl service = (PaasRegisteredServiceImpl)svr;    	
        	RegisteredServiceEntity entity = new RegisteredServiceEntity();
        	entity.setId(service.getId());    	
        	entity.setServiceKey(service.getServiceId());
        	entity.setServiceName(service.getName());
        	entity.setServiceSecret(service.getServiceSecret());    	
        	entity.setDescription(service.getDescription());
        	entity.setEvaluationOrder(service.getEvaluationOrder());
        	entity.setLogoutCallbackUrl(service.getLogoutCallbackUrl());
        	entity.setAuthorities(service.getAuthorities());
        	return entity;
    	}
    	else
    		return null;

    }

    private RegisteredService convert(RegisteredServiceEntity entity){
    	if (entity != null){
        	PaasRegisteredServiceImpl service  = new PaasRegisteredServiceImpl();
        	service.setId(entity.getId());
        	service.setName(entity.getServiceName());
        	service.setServiceId(entity.getServiceKey());
        	service.setServiceSecret(entity.getServiceSecret());
        	service.setDescription(entity.getDescription());
        	service.setEvaluationOrder(entity.getEvaluationOrder());
        	service.setLogoutCallbackUrl(entity.getLogoutCallbackUrl());  
        	service.setAuthorities(entity.getAuthorities());
        	return service;
    	}
    	else
    		return null;

    }   
    
    private long findHighestId() {
        long id = 0;
        List<RegisteredService> registeredServices = load();
        for (final RegisteredService r : registeredServices) {
            if (r.getId() > id) {
                id = r.getId();
            }
        }

        return id;
    }

}
